import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/meter_reading/submit_meter_reading_page.dart';
import 'package:ls_work/viewmodel/meter_reading/meter_readinng_room_view_model.dart';
import 'package:ls_work/widgets/bottom_input_widget.dart';

import '../../widgets/bottom_chart_input_widget.dart';
import '../../widgets/drop_item_widget.dart';

class MeterReadingRoomPage extends BaseStatefulMvvmWidget {
  final String? buildingName;
  final String? floorName;
  final String? buildingId;
  final String? floorId;
  final dynamic type;

  MeterReadingRoomPage(
      {this.buildingName,
      this.floorName,
      this.buildingId,
      this.floorId,
      this.type});

  @override
  State<StatefulWidget> createState() => _MeterReadingRoomState();
}

class _MeterReadingRoomState
    extends BaseMvvmListState<MeterReadingRoomViewModel, MeterReadingRoomPage>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return loadingContentWidget;
  }

  @override
  Widget buildLoadingContentView() {
    return Column(
      children: [
        Stack(
          alignment: Alignment.center,
          children: [
            CommonText('${widget.buildingName}-${widget.floorName}'),
            ElevatedButton(
                    onPressed: () {
                      viewModel.submitReading();
                    },
                    child: Text('提交'))
                .setLocation(right: 16)
          ],
        ).addToContainer(
          height: 50,
          color: Colours.bg_color,
          width: double.infinity,
        ),
        Container(
          height: 10,
        ),
        Row(
          children: [
            CommonText('房间号', textAlign: TextAlign.center).setWeight(1),
            CommonText('上次抄表', textAlign: TextAlign.center).setWeight(1),
            CommonText('本次抄表', textAlign: TextAlign.center).setWeight(1),
          ],
        ).addToContainer(
            padding: EdgeInsets.symmetric(vertical: 14, horizontal: 16),
            color: Colors.grey[200]),
        super.buildLoadingContentView()!.setWeight(1)
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return initProvider();
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Row(
      children: [
        CommonText('${item.roomNo}', textAlign: TextAlign.center).setWeight(1),
        CommonText('${item.number ?? '--'}', textAlign: TextAlign.center)
            .setWeight(1),
        CommonText('${item.ordinalNumber ?? ""}',
                hintText: '本次抄表', textAlign: TextAlign.center)
            .addToContainer(
                padding: EdgeInsets.symmetric(vertical: 4, horizontal: 10),
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                    borderRadius: 5.borderRadius))
            .onTap(() {
          showMeterReadingBottomInput(context,
              title: "本次抄表",
              content: item.ordinalNumber ?? "",
              selectType: item.meterReadingType,
              keyboard: CommonInputType.numberAndDecimal, onSubmit: (value) {
            setState(() {
              item.ordinalNumber = value.reading;
              item.readingTime = value.readTime;
              item.meterReadingType = value.readingType?.pickId;
            });
          });
        }).setWeight(1),
      ],
    ).addToContainer(
        padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
        color: Colors.white);
  }

  @override
  MeterReadingRoomViewModel createViewModel() {
    return MeterReadingRoomViewModel(context,
        buildingId: widget.buildingId??"",
        floorId: widget.floorId??"",
        type: widget.type);
  }

  @override
  bool get wantKeepAlive => true;
}
